import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_file_upload_page.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/core/utils/file_util.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';

import 'controller.dart';

///已完成
class CompletedStatePage extends StatelessWidget {
  CompletedStatePage({super.key});

  final controller = Get.put(CompletedStateController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.bg_color_grey,
      appBar: CustomAppBar(
        titleText: '已完成',
      ),
      body: GetBuilder(
        init: controller,
        builder: (_) {
          return SingleChildScrollView(
            child: Column(
              children: [
                _titleBg(),
                Container(
                  height: 40.w,
                  width: double.infinity,
                  color: const Color(0xFFF4F7FF),
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 40.w),
                  child: Text(
                    '隐患信息',
                    style: TextStyle(
                      fontSize: 16.sp,
                    ),
                  ),
                ),
                Container(
                  height: 1.w,
                  color: const Color(0xFFDEE5FD),
                ),
                Container(
                  color: Colors.white,
                  padding: EdgeInsets.only(
                      left: 20.w, right: 20.w, top: 20.w, bottom: 20.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _rowData(
                          '隐患名称：',
                          controller
                              .preventiveRectificationGetEntity.preventiveName),
                      AppGaps.vGap10,
                      _rowData('责任单位：', controller.preventiveRectificationGetEntity.companyName),
                      AppGaps.vGap10,
                      _rowData('责任部门：', controller.preventiveRectificationGetEntity.deptNames),
                      AppGaps.vGap10,
                      _rowData('责任人：', controller.preventiveRectificationGetEntity.responsibleUserNames),
                      AppGaps.vGap10,
                      _rowData(
                          '发现时间：',
                          controller
                              .preventiveRectificationGetEntity.findTime!=0? Jiffy.parseFromMillisecondsSinceEpoch(controller
                              .preventiveRectificationGetEntity.findTime)
                              .format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                      AppGaps.vGap10,
                      _rowData(
                          '整改期限：',
                          controller
                              .preventiveRectificationGetEntity
                              .rectificationDeadline!=0? Jiffy.parseFromMillisecondsSinceEpoch(controller
                              .preventiveRectificationGetEntity
                              .rectificationDeadline)
                              .format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                      AppGaps.vGap10,
                      _rowData('整改人：', controller.preventiveRectificationGetEntity.rectificationUserNames),
                      AppGaps.vGap10,
                      _rowData('整改部门', controller.preventiveRectificationGetEntity.rectificationDeptNames),
                      AppGaps.vGap10,
                      _rowData('隐患来源：',
                          controller.preventiveRectificationGetEntity.source),
                      AppGaps.vGap10,
                      _rowData(
                          '问题描述：',
                          controller
                              .preventiveRectificationGetEntity.problemDesc),
                      AppGaps.vGap10,
                      _rowData(
                          '整改要求：',
                          controller.preventiveRectificationGetEntity
                              .rectificationRequire),
                      AppGaps.vGap12,

                      // _getBottomView()
                      CustomFileUploadPage(
                        isRequired: false,
                        isShowDivider: false,
                        isEnabled: false,
                        isChange: false,
                        uploadedFiles:  controller.preventiveRectificationGetEntity.fileUrl.isNotEmpty?  controller.preventiveRectificationGetEntity.fileUrl.split(','):[],
                        title: '附件：',
                        onCallBack: (value) {},
                      ),
                    ],
                  ),
                ),
                _titleBg(),
                Container(
                  height: 40.w,
                  width: double.infinity,
                  color: const Color(0xFFF4F7FF),
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 40.w),
                  child: Text(
                    '整改信息',
                    style: TextStyle(
                      fontSize: 16.sp,
                    ),
                  ),
                ),
                Container(
                  height: 1.w,
                  color: const Color(0xFFDEE5FD),
                ),
                Column(
                  children: controller
                      .preventiveRectificationGetEntity.recordDOS
                      .map((e) {
                    return Container(
                      color: Colors.white,
                      padding: EdgeInsets.only(
                          left: 20.w, right: 20.w, top: 20.w, bottom: 20.w),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          _rowData('整改人：', e.rectificationPersonName),
                          AppGaps.vGap10,
                          _rowData('整改时间：',e.rectificationTime!=0? Jiffy.parseFromMillisecondsSinceEpoch( e.rectificationTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                          AppGaps.vGap10,
                          _rowData('完成情况：','${e.rectificationProgress}%'),
                          AppGaps.vGap10,
                          _rowData('整改进度：', e.rectificationDesc),
                          AppGaps.vGap10,
                          _rowData('整改措施：', e.rectificationActions),
                          AppGaps.vGap12,
                          // _getBottomView(),

                          CustomFileUploadPage(
                            isRequired: false,
                            isShowDivider: false,
                            isEnabled: false,
                            isChange: false,
                            uploadedFiles: e.rectificationFile.isNotEmpty? e.rectificationFile.split(','):[],
                            title: '附件：',
                            onCallBack: (value) {},
                          ),
                        ],
                      ),
                    );
                  }).toList(),
                ),

                _titleBg(),
                Container(
                  height: 40.w,
                  width: double.infinity,
                  color: const Color(0xFFF4F7FF),
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 40.w),
                  child: Text(
                    '验收信息',
                    style: TextStyle(
                      fontSize: 16.sp,
                    ),
                  ),
                ),
                Container(
                  height: 1.w,
                  color: const Color(0xFFDEE5FD),
                ),
                Container(
                  color: Colors.white,
                  padding: EdgeInsets.only(
                      left: 20.w, right: 20.w, top: 20.w, bottom: 20.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _rowData('验收情况：',  controller.preventiveRectificationGetEntity.acceptanceStatus==1?'合格':'不合格'),
                      AppGaps.vGap10,
                      _rowData('验收时间：', controller.preventiveRectificationGetEntity.approvalTime!=0? Jiffy.parseFromMillisecondsSinceEpoch( controller.preventiveRectificationGetEntity.approvalTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                      AppGaps.vGap10,
                      _rowData('验收描述：',  controller.preventiveRectificationGetEntity.acceptanceDesc),
                      AppGaps.vGap12,
                      // _getBottomView(),
                      CustomFileUploadPage(
                        isRequired: false,
                        isShowDivider: false,
                        isEnabled: false,
                        isChange: false,
                        uploadedFiles: controller.preventiveRectificationGetEntity.acceptanceFileUrl.isNotEmpty?  controller.preventiveRectificationGetEntity.acceptanceFileUrl.split(','):[],
                        title: '附件：',
                        onCallBack: (value) {},
                      ),
                    ],
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget _rowData(String name, String? value,
      {Color valueColor = const Color(0xFF495061)}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            height: 1.2,
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Expanded(
            child: Text(
          value ?? '空',
          style: TextStyle(
            color: valueColor,
            height: 1.2,
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )),
      ],
    );
  }

  /// 标题背景分隔栏
  Widget _titleBg() {
    return Column(
      children: [
        AppGaps.vGap12,
        Container(
          height: 2.w,
          color: const Color(0xFF587DF5),
        ),
      ],
    );
  }

  Widget _getBottomView() {
    return Column(
      children: [
        Text(
          '拍照:',
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xffA5A6B5),
            overflow: TextOverflow.ellipsis,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.vGap6,
        Wrap(
            spacing: 5.w,
            children: controller.dangerImages!.map<Widget>((item) {
              return Container(
                width: 74.w,
                height: 74.w,
                margin: EdgeInsets.only(bottom: 10.w),
                decoration: BoxDecoration(
                  color: const Color(0xffF7F7FB),
                  border: Border.all(
                    color: const Color(0xffEBEDF0),
                  ),
                  borderRadius: BorderRadius.circular(8.w),
                  image: DecorationImage(
                    image: NetworkImage(Interface.fileUrl + item),
                    fit: BoxFit.cover,
                  ),
                ),
              ).clickShowLarge(url: Interface.fileUrl + item);
            }).toList()),
        AppGaps.vGap12,
        Text(
          '视频:',
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xffA5A6B5),
            overflow: TextOverflow.ellipsis,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.vGap6,
        Wrap(
            spacing: 5.w,
            children: controller.dangerVideo!.map<Widget>((item) {
              return Container(
                width: 160.w,
                height: 90.w,
                margin: const EdgeInsets.only(bottom: 10),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: const Color(0xff060B15),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: IconButton(
                  icon: const Icon(
                    Icons.play_circle,
                    color: Colors.white,
                    size: 30,
                  ),
                  onPressed: () {
                    FileUtils.openFile(Interface.fileUrl + item);
                  },
                ),
              );
            }).toList()),
      ],
    );
  }
}
